package com.dc.spike.mapper;

import com.dc.spike.mapper.entity.SeckillEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface SeckillMapper {

    @Select("SELECT seckill_id AS seckillId,name as name,inventory as inventory,start_time as startTime,end_time as endTime,create_time as createTime,version as version from dc_seckill where seckill_id=#{seckillId}")
    SeckillEntity findBySeckillId(Long seckillId);

    /**
     * 如何实现库存防止超卖呢？
     * 乐观锁
     *
     * @param seckillId
     * @return
     */
    @Update("update dc_seckill set inventory=inventory-1, version=version+1 where  seckill_id=#{seckillId} and inventory>0  and version=#{version} ;")
    int inventoryDeductionClock(@Param("seckillId") Long seckillId, @Param("version") Long version);

    /**
     * 数据库自带行锁，不会产生两个线程访问同一个数据
     * 非乐观锁
     */
    @Update("update dc_seckill set inventory=inventory-1 where seckill_id=#{seckillId} AND inventory>0 ;")
    int inventoryDeductionUnclock(@Param("seckillId") Long seckillId);

}